//
//  UITableViewCellWithIconList.m
//  Money Lover
//
//  Created by Hung Nguyen Thanh on 10/6/11.
//  Copyright (c) 2011 UET. All rights reserved.
//

#import "UITableViewCellWithIconList.h"

@implementation UITableViewCellWithIconList

@synthesize listIcon, listButton;
@synthesize topIndex, numOfIconInCell, delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier numOfIconInCell:(NSInteger)limit{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        numOfIconInCell = limit;
        listButton = [[NSMutableArray alloc] init];
        UIButton *button = nil;
        for (int i = numOfIconInCell; i > 0; i--) {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            [listButton addObject:button];
            [button addTarget:self action:@selector(iconSelected:) forControlEvents:UIControlEventTouchUpInside];
            [button.imageView setContentMode:UIViewContentModeScaleAspectFill];
            [self.contentView addSubview:button];
        }
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setupIcon{
    @try {
        
        NSInteger indexOfIcon = numOfIconInCell * topIndex;
        NSInteger numOfIcon = [listIcon count];
        NSInteger limit = indexOfIcon + numOfIconInCell;
//        NSLog(@"IndexOfIcon = %d", indexOfIcon);
        CGRect rect = CGRectMake(10, 5, 90, 110);
        UIButton *imageView = nil;
        Photo *icon = nil;
        UIImage *image = nil;
//        NSString *appDir = [[NSBundle mainBundle] resourcePath];
        int j = numOfIconInCell - 1;
        for (int i = indexOfIcon; i < limit; i++) {
            
            if (i  >= numOfIcon) {
                imageView = [listButton objectAtIndex:j--];
                [imageView setImage:nil forState:UIControlStateNormal];
                imageView.tag = -1;
                continue;
            }
            icon = [listIcon objectAtIndex:i ];
            if (icon == nil) {
                continue;
            }
//            NSLog(@"icon: %@", icon);
            imageView = [listButton objectAtIndex:j--];
            image = icon.thumbImage;
            if (image == nil) {
                continue;
            }
            [imageView setImage:image forState:UIControlStateNormal];
            imageView.frame = rect;
            imageView.tag = i;
            
            rect.origin.x += 100;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Error: %@", exception);
    }
    @finally {
        
    }
    
    
    
}

- (IBAction)iconSelected:(id)sender{
    if (((UIButton *)sender).tag == -1) {
        return;
    }
//    NSLog(@"Selected icon: %@", [listIcon objectAtIndex:((UIButton *)sender).tag]);
    if ([delegate respondsToSelector:@selector(didSelectedIcon:)]) {
        [delegate didSelectedIcon:[listIcon objectAtIndex:((UIButton *)sender).tag]];
    }
}

@end
